package com.dvt.faceScan;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.dvt.faceScan.httpUntil.XmlRpcUtils;
import com.dvt.faceScan.widget.Config;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;


public class ChoiseCompany extends Activity {
    private TextView tv1;//item.xml里的TextView：Textviewname
    private TextView tv2;//item.xml里的TextView：Textviewage

    private ListView lv;//activity_main.xml里的ListView
    private BaseAdapter adapter;//要实现的类
    private ArrayList<employeeModel> gzList = new ArrayList<employeeModel>();//实体类

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.choisecompany_activity);
        lv = (ListView) findViewById(R.id.listView3);
        requestCompaniesList();

        adapter = new BaseAdapter() {
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return gzList.size();//数目
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                LayoutInflater inflater = ChoiseCompany.this.getLayoutInflater();
                View view;
                if (convertView == null) {
                    //因为getView()返回的对象，adapter会自动赋给ListView
                    view = inflater.inflate(R.layout.item_xzgs, null);
                } else {
                    view = convertView;
                    Log.i("info", "有缓存，不需要重新生成" + position);
                }
                tv1 = (TextView) view.findViewById(R.id.Textviewgs);//找到Textviewname
                tv1.setText(gzList.get(position).getCompany());//设置参数

                tv2 = (TextView) view.findViewById(R.id.Textviewgoo);//找到Textviewname
                tv2.setText(gzList.get(position).getCompanyId());//设置参数

                return view;
            }

            @Override
            public long getItemId(int position) {//取在列表中与指定索引对应的行id
                return 0;
            }

            @Override
            public Object getItem(int position) {//获取数据集中与指定索引对应的数据项
                return null;
            }
        };
        lv.setAdapter(adapter);

        //获取当前ListView点击的行数，并且得到该数据
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                tv1 = (TextView) view.findViewById(R.id.Textviewgs);//找到Textviewname
                tv2 = (TextView) view.findViewById(R.id.Textviewgoo);
                String str = tv1.getText().toString();//得到数据
                //Toast.makeText(Gzcx.this, "点击..." + str, Toast.LENGTH_SHORT).show();//显示数据
                Intent intent = new Intent();
                intent.putExtra("gs", str);
                intent.putExtra("gsid", tv2.getText().toString());
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });

    }

    //调公司列表接口
    private void requestCompaniesList() {

        Callable<String> c = new Callable<String>() {
            @Override
            public String call() {
                int uid = 1;
                String db = Config.DB;
                String uname = Config.account;
                String pwd = Config.password;
                String model = "dris.hired_company";
                String odooUrl = Config.baseURL;
                String url = String.format("%s/xmlrpc/2/object", odooUrl);

                Map<String, Object> context = new HashMap<String, Object>();
                context.put("lang", "zh_CN");
                context.put("tz", "Asia/Hong_Kong");
                context.put("uid", uid);
                List args = new ArrayList();

                Map<String, Object> kwargs = new HashMap<String, Object>();
                kwargs.put("context", context);
                kwargs.put("limit", 8);

                Object requestObj = XmlRpcUtils.getXMLRPC(url, "execute_kw", asList(db, uid, pwd, model, "name_search", args,kwargs));
                List r = asList((Object[]) requestObj);
                Log.d("wumh_debug", "报文：" + r);

                Object[] o = (Object[]) requestObj;
                List<Object> ol = asList(o);
                for (Object _o : ol) {
                    Object[] bb = (Object[]) _o;
                    List<Object> cc = asList(bb);
                    String company_id = cc.get(0).toString();
                    String str =  cc.get(1).toString();
                    Log.d("wumh_debug", ""+ str);
                    employeeModel gz = new employeeModel();//给实体类赋值
                    gz.setCompany(str);
                    gz.setCompanyId(company_id);
                    gzList.add(gz);
                }

                for (Object object : r) {
                    Map<String, Object> a = (HashMap) object;
                    employeeModel employee = new employeeModel();
                    String astr = String.valueOf(a.get("id"));
                    String astr2 = String.valueOf(a.get("result"));
                    Log.d("wumh_debug", "报文：" +astr+"|"+astr2);
                    Set<String> akeySet = a.keySet();
                    for (String key : akeySet) {
                        Log.d("mmmmmmmm", key + "-->" + a.get(key));
                    }
                    gzList.add(employee);
                }
                return "";
            }
        };

        ExecutorService pool = Executors.newSingleThreadExecutor();
        Future<String> future = pool.submit(c);
        try {
            String result = future.get(5000, TimeUnit.SECONDS);
            Log.d("vvvvvv", result);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private List asList(Object ...a){
        return Arrays.asList(a);
    }

}